//初始化树结构（合并初始化）== 组织管理
var NodeSettingManagement;
NodeSettingManagement = $.extend({}, setting1, setting2);
var orgTree_ = new VanyBaseTree("treeDemo", baseUrl + "/base/org/queryOrgTree?token=" + sessionStorage.token, NodeSettingManagement);
orgTree_.complete(function(e) {
    Jurisdiction();
    //判断是否展示操作区域按钮
    JurisdictionBefore("#treeTitle", "#rMenu");
    try {
        var seletedOrgNode = zTree.getSelectedNodes()[0];
        table_.orgCode = seletedOrgNode.id;
        table_.getData();
    } catch(e) {
        console.log(e)
    }
});
var table_ = new Vue({
	el:"#signNature",
	data:{
           ruleForm:{
               deptSignId:"",
               orgCode:"",
               signContent:"",
               isTopSign:"0",
               topSign:false,
               orgMailId:"",
               maillAccount:"",
               mailPassword:"",
               mailServerAddress:"",
               mailServerPort:0,
           },
        topSign:false,
        rules: {
            signContent: [
                { required: true, message: '请输入签名名称', trigger: 'blur,change' },
                { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'change' }
            ],
            maillAccount: [
                { required: true, message: '请输入发件箱用户名', trigger: 'blur,change' },
                {type:'email', message: '请输入邮箱格式不对',trigger: 'change'}
            ],
            mailPassword: [
                {required: true, message: '请输入发件箱密码', trigger: 'blur,change' }
            ],
            mailServerAddress: [
                {  required: true, message: '请输入发件箱服务器地址', trigger: 'blur,change' }
            ],
            mailServerPort: [
                {  required: true, message: '请输入服务端口号', trigger: 'blur,change' }
             //   {   type: 'number', message: '服务端口号必须为数字',trigger: 'change'}
            ]
        }
    },
    computed:{
        isTopSign:function () {
            if(this.topSign)
                return '1'
            else
                return '0';
        }
    },
    methods:{
	    getData:function () {

            VanyAjax("deptSign/getSignByOrgCode",{orgCode:this.orgCode},"GET", table_.el).done(function(data) {
                if(data!=null)
                {
                    table_.ruleForm.deptSignId=data.deptSignId;
                    table_.ruleForm.orgCode=data.orgCode;
                    table_.ruleForm.signContent=data.signContent;
                    table_.ruleForm.isTopSign=data.isTopSign;
                    table_.ruleForm.topSign=data.topSign;
                    table_.ruleForm.orgMailId=data.orgMailId;
                    table_.ruleForm.maillAccount=data.maillAccount;
                    table_.ruleForm.mailPassword=data.mailPassword;
                    table_.ruleForm.mailServerAddress=data.mailServerAddress;
                    table_.ruleForm.mailServerPort=data.mailServerPort;
                    if(data.isTopSign==='0')
                        table_.topSign=false;
                    else
                        table_.topSign=true;
                }
                else
                {
                    table_.ruleForm.deptSignId="",
                    table_.ruleForm.signContent="",
                    table_.ruleForm.isTopSign="0",
                    table_.ruleForm.topSign=false,
                    table_.ruleForm.orgMailId="",
                    table_.ruleForm.maillAccount="",
                    table_.ruleForm.mailPassword="",
                    table_.ruleForm.mailServerAddress="",
                    table_.ruleForm.mailServerPort=8080
                    table_.topSign=false

                }
            },false);
        },
        addSign:function (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$confirm("确定将部门签名更新", "提示", {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(function(){
                        VanyAjax("deptSign/updateDeptSign",{
                            deptSignId:table_.ruleForm.deptSignId,
                            orgCode:table_.ruleForm.orgCode,
                            signContent:table_.ruleForm.signContent,
                            isTopSign:table_.ruleForm.isTopSign,
                            topSign:table_.topSign,
                            orgMailId:table_.ruleForm.orgMailId,
                            maillAccount:table_.ruleForm.maillAccount,
                            mailPassword:table_.ruleForm.mailPassword,
                            mailServerAddress:table_.ruleForm.mailServerAddress,
                            mailServerPort:table_.ruleForm.mailServerPort
                        },"POST",table_.el).done(function(data) {
                            orgTree_ = new VanyBaseTree("treeDemo", baseUrl + "/base/org/queryOrgTree?token=" + sessionStorage.token, NodeSettingManagement);
                        });
                    }).catch(function(){

                    })
                } else {
                    return false;
                }
            });


        },
        reset:function (formName) {
                table_.ruleForm.signContent="";
                table_.ruleForm.topSign=false;
                table_.ruleForm.isTopSign="0";
                table_.ruleForm.maillAccount="";
                table_.ruleForm.mailPassword="";
                table_.ruleForm.mailServerAddress="";
                table_.ruleForm.mailServerPort=0
                table_.topSign=false
        }
    }
});

/**
 * 函数节流器，在时间间隔内不触发函数（优化）
 * 应用场景：需要反复重复执行的事件，如快速重复点击按钮等等
 * method: 函数 function a(){}
 * throttle(a)
 */
function throttle(method, context) {
	clearTimeout(method.tId);
	method.tId = setTimeout(function() {
		method.call(context);
	}, 100);
}

/**
 * 判断一个可滚动元素是否滚动到了底部
 * e: 元素
 * fn: 滚动到底部需要执行的回调
 * isScrollToBottom(div, function(){})
 */
function isScrollToBottom(e, fn) {
    // e.srcElement存在兼容性问题，并非标准特性
    // https://developer.mozilla.org/zh-CN/docs/Web/API/Event/target
    // 支持 IE6-8
    var target_ = e.target || e.srcElement,
        // 距离顶部高度
        scrollTop = target_.scrollTop,
        // 滚动区域高度
        scrollHeight = target_.scrollHeight,
        // 文档元素高度
        clientHeight = target_.clientHeight;
    // 文档坐标和视口坐标转换，判断元素滚动到了底部
    if (scrollTop + clientHeight == scrollHeight) {
        if (fn) {
            fn.call();
        }
    }

}
/**
 * 将源对象合并到目标对象中去——内部方法
 * src: 源对象
 * to: 目标对象
 * return 目标对象
 * a = {"a": "a"}
 * b = {"b": "b"}
 * extend_(b, a) → {a: "a", b: "b"}
 */
function extend_(srcObj, toObj) {
	for(var key in srcObj) {
		toObj[key] = toObj[key];
	}
	return toObj;
}

/**
 * 判断是否为字符串
 * o: 对象
 * isString_("string") → true
 * isString_([]) → false
 */
function isString_(o) {
	return Object.prototype.toString.call(o) === "[object String]";
}


function message(el,msg,type) {
    el.$message({
        message:msg,
        type:type
    });
}